package com.our.base.easylog.logback.appender;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.AppenderBase;
import ch.qos.logback.core.Layout;
import ch.qos.logback.core.LogbackException;
import ch.qos.logback.core.filter.Filter;
import com.our.base.easylog.IPushLog;
import com.our.base.easylog.buffer.cache.CachePool;
import com.our.base.easylog.entity.LogInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 日志推送Appender
 *
 * @author Alice
 * @date 2023/11/29
 **/
@Component
public class PushLogAppender extends AppenderBase<ILoggingEvent> {

    private Layout<ILoggingEvent> layout;
    CachePool cachePool;

    @Override
    public void start() {
        super.start();
        cachePool = CachePool.getCachePool();
    }

    @Override
    protected void append(ILoggingEvent eventLog) {

        //拿到日志
        String log = layout.doLayout(eventLog);
        eventLog.prepareForDeferredProcessing();

        LogInfo logInfo = new LogInfo();
        logInfo.setLog(log);
        logInfo.setLogTime(new Date());
        cachePool.writeOneData(logInfo);
    }

    public Layout<ILoggingEvent> getLayout() {
        return layout;
    }

    public void setLayout(Layout<ILoggingEvent> layout) {
        this.layout = layout;
    }
}

